﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace shopform
{
    public partial class ModifyClientReferentForm : Form
    {
        public ModifyClientReferentForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Populates combobox after choosing the right company.
        /// </summary>
        private void feltoltCB()
        {
            DataTable dt = CompanyReferent.getAll(this.TBCompanyID.Text);

            this.CBCompanyReferent.DataSource = dt;
            this.CBCompanyReferent.DisplayMember = "CompanyReferentName";
            this.CBCompanyReferent.ValueMember = "CompanyReferentMail";
            this.CBCompanyReferent.SelectedIndex = -1;
        }

        /// <summary>
        /// Loads data into fields.
        /// </summary>
        /// <param name="c">CompanyReferent model.</param>
        private void loadDataIntoForm(CompanyReferent c)
        {
            this.TBCompRefMail.Text = c.CompanyReferentMail;
            this.TBCompRefName.Text = c.CompanyReferentName;
            this.TBCompRefPhone.Text = c.CompanyReferentPhoneNr;
        }

        /// <summary>
        /// Updates information about company referent.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonInsert_Click(object sender, EventArgs e)
        {
            try
            {
                CompanyReferent c = new CompanyReferent();
                c.CompanyReferentName = this.TBCompRefName.Text.ToString();
                c.CompanyReferentPhoneNr = this.TBCompRefPhone.Text.ToString();
                c.CompanyID = this.TBCompanyID.Text.ToString();
                c.CompanyReferentMail = this.TBCompRefMail.Text.ToString();

                c.updateCompanyReferent();
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// When user clicks Search Company button this is what gonna happen.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonSearchCompany_Click(object sender, EventArgs e)
        {
            try
            {
                String selectedCompany;
                CompanyIDSelectorForm aCompanyIDSelector = new CompanyIDSelectorForm();
                aCompanyIDSelector.ShowDialog();
                selectedCompany = aCompanyIDSelector.getSelectedCompanyID();
                aCompanyIDSelector.Dispose();

                if (selectedCompany != "none")
                {
                    this.TBCompanyID.Text = selectedCompany;
                }

                this.feltoltCB();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Hiba\n" + ex.Message);
            }
        }

        private void ButtonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CBCompanyReferent_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.CBCompanyReferent.SelectedIndex == -1)
            {
                return;
            }
            CompanyReferent aCompanyReferent = 
                new CompanyReferent(
                   this.CBCompanyReferent.SelectedValue.ToString()
                );
            this.loadDataIntoForm(aCompanyReferent);
        }

    }//class

}//namespace
